#include "AsyncIOControl.h"
#include "utils\Logger.h"
AsyncIOControl::AsyncIOControl() {
	scanTime = 1000; 

	rejectBackTime = 200;
	delaytime = 2200;
	consecutiveVal = true;
	signalizationVal = true;
	hasRejectBelt = true;
	hasFeedAgency = true;

	hasReject = false;
	buzzerPort = 1;
	airPort = 2;
	rejectPort = 3;
	feedPort = 6;
	rejectBeltPort = 4;

	hasConnect = false;
	hasOpen = false;
}

AsyncIOControl::~AsyncIOControl() {
	ioControl = nullptr;
	hasConnect = false;
	hasOpen = false;
	LInfo("~AsyncIOControl");
}

bool AsyncIOControl::HasConnected() {
	LInfo("HasConnected");
	bool ret;
	variableMtx.lock();
	ret = hasConnect; 
	variableMtx.unlock();
	return ret;
}

void AsyncIOControl::onInit() {
	LInfo("onInit");
	if (ioControl == nullptr) {
		ioControl = std::make_shared<IOControl>();
		ioControl->setComNo(comNo);
		ioControl->setScanTime(scanTime);

		ioControl->setBlowTime(delaytime);
		ioControl->setRejectBackTime(rejectBackTime);
		ioControl->setRejectSignalization(signalizationVal);
		ioControl->setRejectConsecutive(consecutiveVal);
		ioControl->setRejectBelt(hasRejectBelt);

		ioControl->setBuzzerConPort(buzzerPort);
		ioControl->setAirConPort(airPort);
		ioControl->setFeedConPort(feedPort);
		ioControl->setRejectConPort(rejectPort);
		ioControl->setRejectBeltPort(rejectBeltPort);

		connect(ioControl.get(), SIGNAL(EmergencyStop(bool)), this, SIGNAL(EmergencyStop(bool)));
		connect(ioControl.get(), SIGNAL(connectFail()), this, SIGNAL(connectFail()));
	}
	bool ret = ioControl->Init();
	
	emit initComplete(ret);
}

void AsyncIOControl::SetComNo(std::string _com){
	LInfo("SetComNo");
	comNo = _com;
}
void AsyncIOControl::SetScanPeriodNo(int scantime) {
	scanTime = scantime;
}
void AsyncIOControl::SetRejectType(int type) {
	if (type >= 0) {
		hasReject = true;
	}
	else {
		hasReject = false;
	}
}
void AsyncIOControl::SetBlowTimeNo(int time) {
	delaytime = time;
}
void AsyncIOControl::SetRejectBackTimeNo(int backTime) {
	rejectBackTime = backTime;
}
void AsyncIOControl::SetRejectConsecutiveVal(bool consecutive) {
	consecutiveVal = consecutive;
}
void AsyncIOControl::SetRejectSignalizationVal(bool signalization) {
	signalizationVal = signalization;
}
void AsyncIOControl::SetHasRejectBelt(bool hasBelt) {
	hasRejectBelt = hasBelt;
}
void AsyncIOControl::SetHasFeedAgency(bool hasFeed) {
	hasFeedAgency = hasFeed;
}


void AsyncIOControl::SetBuzzerConOutPrtNo(int port) {
	buzzerPort = port;
}
void AsyncIOControl::SetFeedConOutPrtNo(int port) {
	feedPort = port;
}
void AsyncIOControl::SetAirConOutPrtNo(int port) {
	airPort = port;
}
void AsyncIOControl::SetRejectConOutPrtNo(int port) {
	rejectPort = port;
}
void AsyncIOControl::SetRejectBeltOutPrtNo(int port) {
	rejectBeltPort = port;
}



void AsyncIOControl::setBlowTime(int time) {
	if (ioControl)
		ioControl->setBlowTime(time);
}
void AsyncIOControl::setRejectBackTime(int backTime) {
	if (ioControl)
		ioControl->setRejectBackTime(backTime);
}
void AsyncIOControl::setScanPeriod(int scantime) {
	scanTime = scantime;
	if (ioControl)
		ioControl->setScanTime(scantime);
}
void AsyncIOControl::setRejectConsecutive(bool consecutive) {
	if (ioControl)
		ioControl->setRejectConsecutive(consecutive);
}
void AsyncIOControl::setRejectSignalization(bool signalization) {
	if (ioControl)
		ioControl->setRejectSignalization(signalization);
}
void AsyncIOControl::setTestMode() {
	if (ioControl)
		ioControl->setTestMode();
}

void AsyncIOControl::onReConnection() {
	LInfo("onConnect");
	bool ret = ioControl->ReConnection();
	emit reConnectComplete(ret);
}

void AsyncIOControl::onGetDevInData() {
	LInfo("onGetDevInData");
	bool ret = ioControl->GetDevInData();
	//emit GetDevInDataComplete(ret);
	variableMtx.lock();
	hasConnect = ret;
	variableMtx.unlock();
}

void AsyncIOControl::onSetAlarmStatus(int _status) {
	LInfo("onSetAlarmStatus");
	bool ret = ioControl->SetAlarmStatus(_status);
	emit SetAlarmComplete(ret);
}
void AsyncIOControl::onSetBuzzerOutPort(int port) {
	ioControl->setBuzzerConPort(port);
}

void AsyncIOControl::onSetAirConStatus(int _status) {
	//LInfo("onSetAlarmStatus");
	bool ret = ioControl->SetAirConStatus(_status);
	emit SetAirConComplete(ret);
}
void AsyncIOControl::onSetAirOutPort(int port) {
	ioControl->setAirConPort(port);
}

void AsyncIOControl::onSetFeedOutPort(int port) {
	if (hasFeedAgency) {
		ioControl->setFeedConPort(port);
	}
}
void AsyncIOControl::onSetRejectOutPort(int port) {
	if (hasReject) {
		ioControl->setRejectConPort(port);
	}
}
void AsyncIOControl::onSetRejectBeltOutPort(int port) {
	if (hasReject && hasRejectBelt) {
		ioControl->setRejectBeltPort(port);
	}
}

void AsyncIOControl::onBlowStart(int idx) {
	LInfo("onBlowStart");
	bool ret = ioControl->BlowControl(idx);
	emit BlowComplete(ret);
}

void AsyncIOControl::onControlRejectBelt(RejectBeltState State) {
	if (hasReject && hasRejectBelt) {
		LInfo("onControlRejectBelt");
		bool ret = ioControl->ControlRejectBelt(State);
		emit ControlRejectBeltComplete(ret);
	}
}
void AsyncIOControl::onControlFeedBelt(RejectBeltState State) {
	if (hasFeedAgency) {
		LInfo("onControlFeedBelt");
		bool ret = ioControl->ControlFeedBelt(State);
		emit ControlFeedBeltComplete(ret);
	}
}
void AsyncIOControl::onSendNGSignal() {
	LInfo("onSendNGSignal");
	if (hasReject) {
		ioControl->SendNGSignal();
	}	
}
void AsyncIOControl::onSendOKSignal() {
	LInfo("onSendOKSignal");
	if (hasReject) {
		ioControl->SendOKSignal();
	}
}
void AsyncIOControl::onSendDiscard() {
	LInfo("onSendDiscard");
	bool ret = ioControl->Discard();
	//emit DiscardComplete(ret);
}

void AsyncIOControl::onSetRejectTime(int rejTime, int rejResTime) {
	if (ioControl) {
		ioControl->setBlowTime(rejTime);
		ioControl->setRejectBackTime(rejResTime);
	}
}

bool AsyncIOControl::GetDiscardStatus() {
	return ioControl->GetDiscardStatus();
}